<?php

$page = "personas";

require 'require.php';

if (!isset($_SESSION['user_id'])) {
	header("Location: /index.php");
	exit;
}

$link_server = mysql_connect(OPENIDOO_DB_SERVER, OPENIDOO_DB_USER, OPENIDOO_DB_PASSWORD);
$select_db = mysql_select_db(OPENIDOO_DB_DATABASE, $link_server);

if (isset($_GET['id']) && ($_GET['id']) && isset($_GET['a']) && ($_GET['a'] == 'del')) {

	if ((!$link_server) or (!$select_db)) {
		die('Impossible de se connecter');
	} else {

		if (!($verif_site = mysql_query("SELECT user_id, persona_id FROM openidoo_personas WHERE persona_id='".mysql_escape_string($_GET['id'])."' LIMIT 1", $link_server))) {
			die('Une erreur est survenue');
		} else {

			if (mysql_num_rows($verif_site) == 1) {
				$verif_site_user_id = mysql_result($verif_site, 0, 'user_id');
			}

			if ($verif_site_user_id == $_SESSION['user_id']) {

				if (!mysql_query("DELETE FROM openidoo_personas WHERE persona_id='".mysql_escape_string($_GET['id'])."' AND user_id='".mysql_escape_string($_SESSION['user_id'])."'", $link_server)) {
					die('Une erreur est survenue');
				}

			}
		}
	}
}

if (isset($_GET['id']) && ($_GET['id']) && isset($_GET['a']) && ($_GET['a'] == 'def')) {

	if ((!$link_server) or (!$select_db)) {
		die('Impossible de se connecter');
	} else {

		if (!mysql_query("UPDATE `openidoo_personas` SET `default`='0' WHERE `user_id`='".mysql_escape_string($_SESSION['user_id'])."'", $link_server)) {
			die('Une erreur est survenue');
		}

		if (!mysql_query("UPDATE `openidoo_personas` SET `default`='1' WHERE `user_id`='".mysql_escape_string($_SESSION['user_id'])."' AND persona_id='".mysql_escape_string($_GET['id'])."' LIMIT 1", $link_server)) {
			die('Une erreur est survenue');
		}

	}
}

if ((!$link_server) or (!$select_db)) {
	die('Impossible de se connecter');
} else {

	if (!($check_personas = mysql_query("SELECT persona_id, user_id, `default`, persona_name, nickname, fullname, email, dob, gender, postcode, country, language, timezone FROM openidoo_personas WHERE user_id='".mysql_escape_string($_SESSION['user_id'])."' ORDER BY  persona_name ASC", $link_server))) {
		die('Une erreur est survenue');
	}

}

require 'html/top.html';
require 'html/personas.html';
require 'html/bottom.html';

?>
